import React from "react";

import Constants from "../common/Constants";
import ToastUtils from "../common/ToastUtils";
import ResponseConstants from "../common/ResponseConstants";

export default class BaseModel {
  constructor() {
    this.dataManager = null;
    this.moduleType = "";
  }

  setup(dataManager) {
    this.dataManager = dataManager;
  }

  sendGet(url, callback) {
    // console.log("sendGet", url);
    var cloneResponse = null;
    fetch(url, {
      method: "GET",
      headers: this._getHeader("GET")
    })
      .then(function(response) {
        // console.log(response);
        cloneResponse = response.clone();
        if (
          cloneResponse.status == ResponseConstants.status_created_success ||
          cloneResponse.status == ResponseConstants.status_noData ||
          cloneResponse.status == ResponseConstants.status_refuse_request ||
          cloneResponse.status == ResponseConstants.status_server_error
        ) {
          return response;
        } else {
          return response.json();
        }
      })
      .then(response => {
        // console.log(response);
        if (
          cloneResponse.status == ResponseConstants.status_request_success ||
          cloneResponse.status == ResponseConstants.status_created_success
        ) {
          callback(response, true, cloneResponse);
        } else {
          callback(response, false, cloneResponse);
        }
      })
      .catch(error => {
        // console.error(error);
        ToastUtils.show("网络错误");
        callback(error, false);
      });
  }

  sendGetNoJSON(url, callback) {
    // console.log("sendGetNoJSON", url);
    fetch(url, {
      method: "GET"
    })
      .then(function(response) {
        return response.text();
      })
      .then(function(responseText) {
        callback(responseText, true);
      })
      .catch(error => {
        // console.error(error);
        ToastUtils.show("网络错误");
        callback(error, false);
      });
  }

  sendPut(url, callback) {
    // console.log("sendPut", url);
    try {
      fetch(url, {
        method: "PUT",
        mode: "cors",
        headers: this._getHeader("PUT")
      })
        .then(function(response) {
          callback(response, true);
        })
        .catch(error => {
          console.error(error);
          callback(error, false);
        });
    } catch (e) {
      ToastUtils.show("网络错误");
    }
  }

  sendPutGetJSON(url, callback) {
    // console.log("sendPutGetJSON", url);
    var cloneResponse = null;
    fetch(url, {
      method: "PUT",
      mode: "cors",
      headers: this._getHeader("PUT")
    })
      .then(function(response) {
        cloneResponse = response.clone();
        if (
          cloneResponse.status == ResponseConstants.status_deal_success ||
          cloneResponse.status == ResponseConstants.status_conflict ||
          cloneResponse.status == ResponseConstants.status_refuse_request ||
          cloneResponse.status == ResponseConstants.status_created_success ||
          cloneResponse.status == ResponseConstants.status_server_error
        ) {
          return response;
        }
        return response.json();
      })
      .then(response => {
        // console.log(response);
        callback(response, true, cloneResponse);
      })
      .catch(error => {
        // console.error(error);
        ToastUtils.show("网络错误");
        callback(error, false, cloneResponse);
      });
  }

  sendPatch(url, data, callback) {
    // console.log("sendPatch", url, data);
    fetch(url, {
      method: "PATCH",
      mode: "cors",
      headers: this._getHeader("PATCH"),
      body: data
    })
      .then(function(response) {
        callback(response, true);
      })
      .catch(error => {
        // console.error(error);
        ToastUtils.show("网络错误");
        callback(error, false);
      });
  }

  sendPatchGetJasn(url, data, callback) {
    // console.log("sendPatchGetJasn", url, data);
    var cloneResponse = null;
    fetch(url, {
      method: "PATCH",
      mode: "cors",
      headers: this._getHeader("PATCH"),
      body: data
    })
      .then(function(response) {
        // console.log("sendPatchGetJasn json", response);
        cloneResponse = response.clone();
        if (
          cloneResponse.status == ResponseConstants.status_deal_success ||
          cloneResponse.status == ResponseConstants.status_conflict ||
          cloneResponse.status == ResponseConstants.status_refuse_request ||
          cloneResponse.status == ResponseConstants.status_created_success ||
          cloneResponse.status == ResponseConstants.status_server_error
        ) {
          return response;
        }
        return response.json();
      })
      .then(response => {
        // console.log(response);
        callback(response, true, cloneResponse);
      })
      .catch(error => {
        // console.error(error);
        ToastUtils.show("网络错误");
        callback(error, false, cloneResponse);
      });
  }

  sendPost(url, data, callback) {
    // console.log("sendPost", url, data);
    var cloneResponse = null;
    fetch(url, {
      method: "POST",
      mode: "cors",
      headers: this._getHeader("POST"),
      body: data
    })
      .then(response => {
        // console.log(response);
        cloneResponse = response.clone();
        callback(response, true, cloneResponse);
      })
      .catch(error => {
        // console.error(error);
        ToastUtils.show("网络错误");
        callback(error, false, cloneResponse);
      });
  }

  sendPostGetJson(url, data, callback) {
    // console.log("sendPostGetJson", url, data);
    var cloneResponse = null;
    fetch(url, {
      method: "POST",
      mode: "cors",
      headers: this._getHeader("POST"),
      body: data
    })
      .then(function(response) {
        // console.log("sendPostGetJson json", response);
        cloneResponse = response.clone();
        if (
          cloneResponse.status == ResponseConstants.status_deal_success ||
          cloneResponse.status == ResponseConstants.status_conflict ||
          cloneResponse.status == ResponseConstants.status_bad_request ||
          cloneResponse.status == ResponseConstants.status_refuse_request ||
          cloneResponse.status == ResponseConstants.status_created_success ||
          cloneResponse.status == ResponseConstants.status_server_error
        ) {
          return response;
        }
        return response.json();
      })
      .then(response => {
        // console.log(response);
        callback && callback(response, true, cloneResponse);
      })
      .catch(error => {
        // console.error(error);
        ToastUtils.show("网络错误");
        callback && callback(error, false, cloneResponse);
      });
  }

  sendPostMustJson(url, data, callback, isToJson) {
    // console.log("sendPostMustJson", url, data);
    var cloneResponse = null;
    fetch(url, {
      method: "POST",
      mode: "cors",
      headers: this._getHeader("POST"),
      body: data
    })
      .then(function(response) {
        return response.json();
      })
      .then(response => {
        // console.log(response);
        callback(response, true);
      })
      .catch(error => {
        // console.error(error);
        ToastUtils.show("网络错误");
        callback(error, false);
      });
  }

  sendDelete(url, data, callback) {
    // console.log("sendDelete", url, data);
    var cloneResponse = null;
    fetch(url, {
      method: "DELETE",
      mode: "cors",
      headers: this._getHeader("DELETE"),
      body: JSON.stringify(data)
    })
      .then(function(response) {
        // console.log("sendDelete json", response);
        cloneResponse = response.clone();
        if (
          cloneResponse.status == ResponseConstants.status_deal_success ||
          cloneResponse.status == ResponseConstants.status_conflict ||
          cloneResponse.status == ResponseConstants.status_refuse_request ||
          cloneResponse.status == ResponseConstants.status_created_success ||
          cloneResponse.status == ResponseConstants.status_server_error
        ) {
          return response;
        }
        return response.json();
      })
      .then(response => {
        // console.log(response);
        callback(response, true, cloneResponse);
      })
      .catch(error => {
        // console.error(error);
        ToastUtils.show("网络错误");
        callback(error, false, cloneResponse);
      });
  }

  //请求链接
  sendRequestFile(url, callback) {
    // console.log("sendRequestFile", url);
    let cloneResponse = null;
    fetch(url)
      .then(response => {
        cloneResponse = response.clone();
        return response.text();
      })
      .then(responseData => {
        responseData = responseData.replace(/[\r\n]/g, "");
        callback(responseData, cloneResponse);
      })
      .catch(error => {
        // console.log(error);
        ToastUtils.show("网络错误");
        callback(null, response);
      });
  }

  _getHeader(type) {
    // console.log(this.dataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "getAutorization"))
    var headers = {};
    switch (type) {
      case "GET":
      case "PUT":
      case "DELETE":
        headers = {
          Authorization: this.dataManager.callMethod(
            Constants.MODULE_TYPE.LOGIN,
            "getAutorization"
          )
        };
        break;
      case "PATCH":
      case "OPTIONS":
      case "POST":
        headers = {
          Authorization: this.dataManager.callMethod(
            Constants.MODULE_TYPE.LOGIN,
            "getAutorization"
          ),
          "Content-Type": "application/json"
        };
        break;
      default:
        headers = {
          Authorization: this.dataManager.callMethod(
            Constants.MODULE_TYPE.LOGIN,
            "getAutorization"
          )
        };
        break;
    }
    return headers;
  }
}
